#
#
 
nodemailer = require("nodemailer")
conf = require("../config").surv


exports.send_mail = (title,body, email_receiver) ->

  if typeof(process.env.NODE_ENV) == "string" and process.env.NODE_ENV.match("develop")
    return
  
  if conf.email_sender.match(/qq.com/)
    service = "QQ"
  else if conf.email_sender.match(/gmail.com/)
    service = "Gmail"
  else
    console.error "#{conf.email_sender} is not supported"
    return

  smtpTransport = nodemailer.createTransport "SMTP",
    service: service
    auth:
      user: conf.email_sender
      pass: conf.email_password

  mailOptions =
    from: "gfmonitor <#{conf.email_sender}>",
    to: email_receiver
    subject: "gfmonitor: #{title}"
    text: "#{new Date().toFormat('YYYYMMDD')}"
    html: body

  smtpTransport.sendMail mailOptions, (err,res) ->
    if err
      console.error "SendMail fail: #{err}"
    else
      console.log "SendMail success"
